/*******************************************************************************
 * Copyright (c) 2010, 2015 git@git.oschina.net:kaiwill/springstage.git
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.qinyeit.serviceapp.entity.system;

import com.google.common.collect.Lists;
import com.qinyetech.springstage.core.entity.AbstractEntity;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.util.List;

/** 
 * ClassName: ManagementPermission <br/>
 * Function: 系统权限 <br/> 
 * date: 2015年10月6日 上午11:20:46 <br/> 
 * 
 * @author wuqing 
 * @version  
 * @since JDK 1.7 
 */
@Entity
@Table(name="management_permissions")
public class ManagementPermission extends AbstractEntity {
	@Column(name="action_url")
	private String actionUrl; //url地址扩展使用

	@Column(name="action_method")
	private String actionMethod; //url对应的httpMethod

//	@ManyToOne(cascade=CascadeType.REFRESH)
//	@JoinColumn(name="parent_id",nullable=true)
//	@JsonIgnore
//	private ManagementPermission parent; //父权限
	@Column(name="parent_id")
	private Long parentId; //父权限

//	@OneToMany(mappedBy="parent", cascade=CascadeType.ALL)
//	@JsonIgnore
//	@OrderBy(value = "displayOrder desc")
//	private Set<ManagementPermission> children;//子权限

	@Transient
	private List<ManagementPermission> children;//子权限

	@Column(name="code",nullable=true)
	private String code; //权限字符串编码 唯一 可以为空

	@Column(name="name",nullable=false)
	@NotBlank(message="名称不能为空")
	private String name;//权限名称

	@Column(name="delete_able")
	private Boolean deleteAble=true; //是否可以被删除,默认可以删除

	@Column(name="type",nullable = false) //权限类型，用于筛选出左侧菜单
	@Enumerated(EnumType.STRING)
	private Type type= Type.Button;//默认为按钮

	@Column(name="icon",nullable = true,length =50) //菜单图标
	private String icon;

	@Column(name="display_order",nullable=false)
	private Integer displayOrder=0; //显示顺序

	@Transient  //子菜单
	private List<ManagementPermission> childMenu= Lists.newArrayList();

	public List<ManagementPermission> getChildMenu() {
		return childMenu;
	}

	public void setChildMenu(List<ManagementPermission> childMenu) {
		this.childMenu = childMenu;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Integer getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(Integer displayOrder) {
		this.displayOrder = displayOrder;
	}

	public String getActionMethod() {
		return actionMethod;
	}

	public void setActionMethod(String actionMethod) {
		this.actionMethod = actionMethod;
	}

	public boolean isDeleteAble() {
		return deleteAble;
	}

	public void setDeleteAble(boolean deleteAble) {
		this.deleteAble = deleteAble;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}

	public List<ManagementPermission> getChildren() {
		return children;
	}

	public void setChildren(List<ManagementPermission> children) {
		this.children = children;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		ManagementPermission other = (ManagementPermission) obj;
		if (this.getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!this.getId().equals(other.getId()))
			return false;
		return true;
	}

	/**
	 * 类型
	 */
	public enum Type{
		//目录，菜单，按钮
		Directory("目录"),Menu("菜单"),Button("按钮");
		private final String text;
		private Type(String text) {
			this.text = text;
		}

		public String getText() {
			return text;
		}
	}
}
